package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishesService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: DeshesController
 * @Author: YuanDing
 * @Date: 2024/2/3 20:30
 * @Description: 菜品管理
 */

@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理")
public class DishesController {

    @Autowired
    DishesService dishesService;

    /**
     * 获取菜品分页数据
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> pageList(DishPageQueryDTO dishPageQueryDTO) {
        Result<PageResult> result = dishesService.getDishesList(dishPageQueryDTO);
        return result;
    }

    /**
     * 更改菜品状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict( value = "dish" , allEntries = true)
    public Result startOrStop(@PathVariable Integer status , Long id) {
        dishesService.startOrStop(status , id);
        return Result.success();
    }

    /**
     * 根据id获取菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getDisheById(@PathVariable Long id) {
        Result result = dishesService.getDisheById(id);
        return result;
    }

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @CacheEvict( value = "dish" , allEntries = true)
    public Result saveDishes(@RequestBody DishDTO dishDTO) {
        dishesService.saveDishes(dishDTO);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public Result getDishByCategoryId(Integer categoryId) {
        Result result = dishesService.getDishByCategoryId(categoryId);
        return result;
    }

    /**
     * 批量删除菜品
     * @return
     */
    @DeleteMapping
    @CacheEvict( value = "dish" , allEntries = true)
    public Result removeDishBatch(String ids) {
        dishesService.removeDishBatch(ids);
        return Result.success();
    }

    @CacheEvict( value = "dish" , allEntries = true)
    @PutMapping
    public Result updateDish(@RequestBody DishDTO dishDTO) {
        dishesService.updateDish(dishDTO);
        return Result.success();
    }
}
